
/**
 * @Author: 陈瑞鹏
 * @Date:   2022-12-05 17:52:40
 * @Last Modified by:   陈瑞鹏
 * @Last Modified time: 2023-01-10 16:10:35
 */
import barrackCtr from "../ctrs/barracks/barrackCtr";
import gameCtr from "../ctrs/gameCtr";
import netCtr from "../ctrs/netCtr";
import BaseGameLayer from "../libs/core/BaseGameLayer";
import { GameFacade } from "../libs/core/GameFacade";
import { PureMVCEvents } from "../libs/core/PureMVCEvents";
import { SceneManager } from "../libs/manager/SceneManager";
import { INotification } from "../libs/pureMvc/Interfaces";
import itemCom from "./itemCom";

const { ccclass, property } = cc._decorator;

@ccclass

export default class detailLayet extends BaseGameLayer {

  @property({ type: cc.Node, tooltip: "item" })
  item: cc.Sprite = null;

  @property({ type: cc.Label, tooltip: "购买所需金币" })
  goldTxt: cc.Label = null;

  @property({ type: cc.Label, tooltip: "描述" })
  descTxt: cc.Label = null;

  @property({ type: cc.Label, tooltip: "攻击力" })
  attackNum: cc.Label = null;

  @property({ type: cc.Label, tooltip: "生命力" })
  hpNum: cc.Label = null;

  @property({ type: cc.Label, tooltip: "采集" })
  havNum: cc.Label = null;

  @property({ type: cc.Label, tooltip: "生成数量" })
  cNum: cc.Label = null;

  @property({ type: cc.Label, tooltip: "移速" })
  speed: cc.Label = null;

  @property({ type: cc.Label, tooltip: "攻击距离" })
  attackArea: cc.Label = null;

  @property({ type: cc.Label, tooltip: "攻速" })
  gsTxt: cc.Label = null;

  @property({ type: cc.Node, tooltip: "解锁按钮" })
  lockBtn: cc.Node = null;

  @property({ type: cc.Node, tooltip: "解锁所需钻石" })
  lockNode: cc.Node = null;

  @property({ type: cc.Label, tooltip: "解锁所需钻石" })
  lockNum: cc.Label = null;

  @property({ type: cc.Node, tooltip: "出战按钮" })
  toWarBtn: cc.Node = null;

  @property({ type: cc.Node, tooltip: "休息按钮" })
  toRestBtn: cc.Node = null;

  onEnable(): void {
    super.onEnable();
  }

  protected onDataChanged(): void {
    console.log(this.customData);
    this.goldTxt.string = `${this.customData.need || 0}`;
    this.descTxt.string = `${this.customData.desc || ""}`;
    this.attackNum.string = `攻击 | ${this.customData.attack || 0}`;
    this.hpNum.string = `生命 | ${this.customData.hp || 0}`;
    this.havNum.string = `采集 | ${this.customData.harvest || 0}`;
    this.cNum.string = `数量 | ${this.customData.cNum || 0}`;
    this.speed.string = `移速 | ${this.customData.speedXs || 1}`;
    this.attackArea.string = `攻距 | ${this.customData.attackAare || 0}`;
    this.gsTxt.string = `攻速 | ${this.customData.gs || 2}`;

    this.drawUi();
  }

  public listNotificationInterests(): string[] {
    return [PureMVCEvents.ON_ZBCOM_UDPATE];
  }

  public handleNotification(notification: INotification): void {
    let evtName = notification.getName();
    let body = notification.getBody();
    switch (evtName) {
      case PureMVCEvents.ON_ZBCOM_UDPATE:
        this.drawUi();
        break;
    }
  }

  drawUi() {

    if (this.customData.isLock) {
      this.lockBtn.active = true;
      this.toWarBtn.active = false;
      this.toRestBtn.active = false;
      this.lockNode.active = true;
      this.lockNum.string = this.customData.lock;
    } else {
      this.lockBtn.active = false;
      this.lockNode.active = false;
      this.toWarBtn.active = true;
      this.toRestBtn.active = false;
      let bg = this.toWarBtn.getChildByName("Background").getComponent(cc.Sprite);
      if (this.customData.isWar) {
        // this.toWarBtn.active = false;
        // this.toRestBtn.active = true;
        cc.resources.load("ele/xx_btn", cc.SpriteFrame, (err, sp: cc.SpriteFrame) => {
          bg.spriteFrame = sp;
        })
      } else {
        // this.toWarBtn.active = true;
        // this.toRestBtn.active = false;
        cc.resources.load("ele/toWar", cc.SpriteFrame, (err, sp: cc.SpriteFrame) => {
          bg.spriteFrame = sp;
        })
      }
    }
    this.item.getComponent(itemCom).drawUi(this.customData);
  }

  onBtnClick(e, key) {
    switch (key) {
      case "lock":
        // this.customData.isLock = false;
        // barrackCtr.ins.getBarrackById(Number(this.customData.id)).isLock = false;
        netCtr.ins.unLock(Number(this.customData.id));
        break;
      case "war":

        if (this.customData.isWar) {
          if (Number(this.customData.id) == 1) {
            let bool = false;
            gameCtr.ins.barracksArr.forEach(ele => {
              console.log("ele", ele);
              if (ele.id == 12) {
                bool = true;
              }
            })
            if (!bool) {
              SceneManager.getInstance().pushLayer("tipLayer", "必须兵种!");
              return;
            }

          }

          if (Number(this.customData.id) == 12) {
            let bool = false;
            gameCtr.ins.barracksArr.forEach(ele => {
              if (ele.id == 1) {
                bool = true;
              }
            })
            if (!bool) {
              SceneManager.getInstance().pushLayer("tipLayer", "必须兵种!");
              return;
            }

          }
          // this.customData.isWar = false;
          // gameCtr.ins.setWarRest(Number(this.customData.id), false);
          netCtr.ins.setState(Number(this.customData.id));
        } else {
          if (gameCtr.ins.isFull()) {
            SceneManager.getInstance().pushLayer("tipLayer", "当前队伍已满!");
            return;
          }
          // this.customData.isWar = true;
          // gameCtr.ins.setWarRest(Number(this.customData.id), true);
          // SceneManager.getInstance().removeLayer("detailLayer");
          netCtr.ins.setState(Number(this.customData.id));
        }

        break;
      case "rest":
        if (Number(this.customData.id) == 1) {
          SceneManager.getInstance().pushLayer("tipLayer", "必须兵种!");
          return;
        }
        // this.customData.isWar = false;
        // gameCtr.ins.setWarRest(Number(this.customData.id), false);
        netCtr.ins.setState(Number(this.customData.id));
        // SceneManager.getInstance().removeLayer("detailLayer");
        break;
    }
    // this.drawUi();
    // GameFacade.getInstance().sendNotification(PureMVCEvents.ON_ZBCOM_UDPATE);
    // if (key == "war" || key == "rest") {
    //   SceneManager.getInstance().removeLayer("detailLayer");
    // }
  }
}